package com.lottery.material.controller.portal.menu;

import com.lottery.material.common.JsonResult;
import com.lottery.material.controller.base.BaseController;
import com.lottery.material.dto.portal.menu.MenuModuleDto;
import com.lottery.material.entity.conf.MenuLevelConfig;
import com.lottery.material.exception.ResponseException;
import com.lottery.material.service.portal.menuLevel.MenuLevelConfigService;
import com.lottery.material.utils.ExceptionUtil;
import com.lottery.material.vo.menu.MenuModuleVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;

/**
 * @Description:    获取菜单栏选项接口（球事和圈子菜单）
 * @Author:         sunny
 * @CreateDate:     2019/04/10 上午 10:49
 */
@RestController
@RequestMapping("/portal/menu")
@Slf4j
public class MenuModuleDisplayController extends BaseController {

    @Autowired
    private MenuLevelConfigService menuLevelConfigService;

    /**
     * description: 显示菜单模块列表
     * author sunny
     * createDate 2019-04-10 下午 2:51
     * @param menuModuleVo
     * @return
     */
    @PostMapping("/showMenuList")
    public JsonResult showMenuList(@Valid MenuModuleVo menuModuleVo) {
        try {
            //根据菜单类型查询首页菜单模块列表
            List<MenuLevelConfig> menuModuleList = menuLevelConfigService.findMenuModuleListByMenuType(Integer.valueOf(menuModuleVo.getMenuType()));

            //封装响应数据
            List<MenuModuleDto> dtoList = new ArrayList<>();
            if(null != menuModuleList && menuModuleList.size() > 0){
                for(int i = 0; i < menuModuleList.size(); i++){
                    MenuModuleDto menuModuleDto = new MenuModuleDto();
                    MenuLevelConfig menuLevelConfig = menuModuleList.get(i);
                    menuModuleDto.setMenuCode(menuLevelConfig.getMenuCode());
                    menuModuleDto.setMenuName(menuLevelConfig.getMenuName());
                    dtoList.add(menuModuleDto);
                }
            }
            return JsonResult.success(dtoList);
        }catch (Exception e){
            log.error("获取菜单模块列表出现异常", ExceptionUtil.logExceptionStack(e));
            throw new ResponseException(500, "网络异常！");
        }
    }
}
